﻿using University.Workshop.Business;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using University.Workshop.Model;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Web;
using University.Workshop.DataAccess;

namespace University.Workshop.Tests
{
    
    
    /// <summary>
    ///This is a test class for ProductRegistrationManagerTest and is intended
    ///to contain all ProductRegistrationManagerTest Unit Tests
    ///</summary>
    [TestClass()]
    public class ProductRegistrationManagerTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for RegisterNewProduct
        ///</summary>
        ///

        [TestMethod()]
        public void SuccesfulRegistrationTest()
        {
            Random rand = new Random();
            string email = "esardaya" + rand.Next(500) + "@hotmail.com";
            DateTime birthday = Convert.ToDateTime("04/05/1992");
            UserRegistrationManager.RegisterUser("minombre", "miapellido", 'M',
                                         birthday, email, "qwerty12", "qwerty12", "Cochabamba",
                                         "Cercado", "7222222", "4401492");
            User user = UserDataAccess.GetUserByEmail(email);
            Product actual;
            string name = "celular Sony Ericsson con todo incluido" + rand.Next(500);
            actual = ProductRegistrationManager.RegisterNewProduct(name, "120", "este celular tienes que comprarlo porque si no lo compras estoy chau viste o no viste", "Electronics", false, "Bolivianos", user.Id, "1");
            Assert.AreNotEqual(null, actual);
            Assert.AreEqual(name, actual.Name);
        }

        [TestMethod()]
        [ExpectedException(typeof(ValidationException))]
        public void BlankFieldsTest()
        {
            string name = string.Empty; // TODO: Initialize to an appropriate value
            string price = string.Empty; // TODO: Initialize to an appropriate value
            string description = string.Empty; // TODO: Initialize to an appropriate value
            string category = "Electronics";
            Collection<string> images = new Collection<string>();
            string currency = "Bolivianos";
            long sellerID = 1;
            Product actual;
            actual = ProductRegistrationManager.RegisterNewProduct(name, price, description, category, true, currency, sellerID, "1");
            Assert.AreNotEqual(null, actual);
            Assert.AreEqual(name, actual.Name);
        }

        [TestMethod()]
        [ExpectedException(typeof(ValidationException))]
        public void IncorrectPriceTest()
        {
            string name = "Some sort of title for the product";
            string price = "-100,600"; // TODO: Initialize to an appropriate value
            string description = "This is an example of a valid description which is long enough to be accepted"; // TODO: Initialize to an appropriate value
            string category = "Electronics";

            Collection<string> images = new Collection<string>();
            string currency = "Bolivianos";
            long sellerID = 1;
            Product actual;
            actual = ProductRegistrationManager.RegisterNewProduct(name, price, description, category, true, currency, sellerID, "1");
            Assert.AreNotEqual(null, actual);
            Assert.AreEqual(name, actual.Name);
        }

        [TestMethod()]
        [ExpectedException(typeof(ValidationException))]
        public void IncorrectQuantityTest()
        {
            string name = "Some sort of title for the product";
            string price = "1060"; // TODO: Initialize to an appropriate value
            string description = "This is an example of a valid description which is long enough to be accepted"; // TODO: Initialize to an appropriate value
            string category = "Electronics";

            Collection<string> images = new Collection<string>();
            string currency = "Bolivianos";
            long sellerID = 1;
            Product actual;
            actual = ProductRegistrationManager.RegisterNewProduct(name, price, description, category, true, currency, sellerID, "abc");
            Assert.AreNotEqual(null, actual);
            Assert.AreEqual(name, actual.Name);
        }

        [TestMethod()]
        [ExpectedException(typeof(ValidationException))]
        public void IncorrectNameTest()
        {
            string name = "Asda";
            string price = "1000"; // TODO: Initialize to an appropriate value
            string description = "This is an example of a valid description which is long enough to be accepted"; // TODO: Initialize to an appropriate value
            string category = "Electronics";
            Collection<string> images = new Collection<string>();
            string currency = "Bolivianos";
            long sellerID = 1;
            Product actual;
            actual = ProductRegistrationManager.RegisterNewProduct(name, price, description, category, true, currency, sellerID, "1");
            Assert.AreNotEqual(null, actual);
            Assert.AreEqual(name, actual.Name);
        }

        [TestMethod()]
        [ExpectedException(typeof(ValidationException))]
        public void IncorrectDescriptionTest()
        {
            string name = "Some sort of title for the product";
            string price = "1000"; // TODO: Initialize to an appropriate value
            string description = "Short Description"; // TODO: Initialize to an appropriate value
            string category = "Electronics";
            Collection<string> images = new Collection<string>();
            string currency = "Bolivianos";
            long sellerID = 1;
            Product actual;
            actual = ProductRegistrationManager.RegisterNewProduct(name, price, description, category, true, currency, sellerID, "1");
            Assert.AreNotEqual(null, actual);
            Assert.AreEqual(name, actual.Name);

        }

        /// <summary>
        ///A test for GetUploadedFiles
        ///</summary>
        [TestMethod()]
        public void GetUploadedFilesTestNull()
        {
            Collection<HttpPostedFile> imageAttempts = null; // TODO: Initialize to an appropriate value
            Collection<HttpPostedFile> expected = new Collection<HttpPostedFile>(); // TODO: Initialize to an appropriate value
            Collection<HttpPostedFile> actual;
            actual = ProductRegistrationManager.GetUploadedFiles(imageAttempts);
            Assert.AreEqual(expected.Count, actual.Count);
        }

        [TestMethod()]
        [ExpectedException(typeof(ValidationException))]
        public void GetUploadedFilesTestIncorrect()
        {
            Collection<HttpPostedFile> imageAttempts = new Collection<HttpPostedFile>(); // TODO: Initialize to an appropriate value
            imageAttempts.Add(null);
            Collection<HttpPostedFile> expected = new Collection<HttpPostedFile>(); // TODO: Initialize to an appropriate value
            Collection<HttpPostedFile> actual;
            actual = ProductRegistrationManager.GetUploadedFiles(imageAttempts);
            Assert.AreEqual(expected.Count, actual.Count);
        }

        /// <summary>
        ///A test for ModifyProduct
        ///</summary>
        [TestMethod()]
        public void ModifyProductSuccess()
        {
            Random rand = new Random();
            string email = "esardaya" + rand.Next(500) + "@hotmail.com";
            DateTime birthday = Convert.ToDateTime("04/05/1992");
            UserRegistrationManager.RegisterUser("minombre", "miapellido", 'M',
                                         birthday, email, "qwerty12", "qwerty12", "Cochabamba",
                                         "Cercado", "7222222", "4401492");
            User user = UserDataAccess.GetUserByEmail(email);
            Product product = ProductRegistrationManager.RegisterNewProduct("celular Sony Ericsson con todo incluido" + rand.Next(500), "120", "este celular tienes que comprarlo porque si no lo compras estoy chau viste o no viste", "Electronics", false, "Dollars", user.Id, "1");
            string name = "New product name"; // TODO: Initialize to an appropriate value
            string price = "1500"; // TODO: Initialize to an appropriate value
            string description = "This is an example of a valid description which is long enough to be accepted"; // TODO: Initialize to an appropriate value
            string category = "Electronics"; // TODO: Initialize to an appropriate value
            bool hasImages = false; // TODO: Initialize to an appropriate value
            string currency = "Bolivianos"; // TODO: Initialize to an appropriate value // TODO: Initialize to an appropriate value
            string status = "OnSale"; // TODO: Initialize to an appropriate value
            Product expected = null; // TODO: Initialize to an appropriate value
            Product actual;
            actual = ProductRegistrationManager.ModifyProduct(name, price, description, category, hasImages, currency, product.ProductId, status, "1");
            Assert.AreNotEqual(expected, actual);
            Assert.AreEqual(product.ProductId, actual.ProductId);
        }

        [TestMethod()]
        [ExpectedException(typeof(ValidationException))]
        public void ModifyProductBlankFields()
        {
            string name = string.Empty; // TODO: Initialize to an appropriate value
            string price = string.Empty; // TODO: Initialize to an appropriate value
            string description = string.Empty; // TODO: Initialize to an appropriate value
            string category = string.Empty; // TODO: Initialize to an appropriate value
            bool hasImages = false; // TODO: Initialize to an appropriate value
            string currency = string.Empty; // TODO: Initialize to an appropriate value
            long productId = 1; // TODO: Initialize to an appropriate value
            string status = string.Empty; // TODO: Initialize to an appropriate value
            Product expected = null; // TODO: Initialize to an appropriate value
            Product actual;
            actual = ProductRegistrationManager.ModifyProduct(name, price, description, category, hasImages, currency, productId, status, "1");
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("Verify the correctness of this test method.");
        }

        [TestMethod()]
        [ExpectedException(typeof(ValidationException))]
        public void ModifyProductIncorrectPrice()
        {
            string name = "New product name"; // TODO: Initialize to an appropriate value
            string price = "abcd"; // TODO: Initialize to an appropriate value
            string description = "This is an example of a valid description which is long enough to be accepted"; // TODO: Initialize to an appropriate value
            string category = "Electronics"; // TODO: Initialize to an appropriate value
            bool hasImages = false; // TODO: Initialize to an appropriate value
            string currency = "Bolivianos"; // TODO: Initialize to an appropriate value
            long productId = 1; // TODO: Initialize to an appropriate value
            string status = "OnSale"; // TODO: Initialize to an appropriate value
            Product expected = null; // TODO: Initialize to an appropriate value
            Product actual;
            actual = ProductRegistrationManager.ModifyProduct(name, price, description, category, hasImages, currency, productId, status, "1");
            Assert.AreNotEqual(expected, actual);
            Assert.AreEqual(productId, actual.ProductId);
        }

        [TestMethod()]
        [ExpectedException(typeof(ValidationException))]
        public void ModifyProductIncorrectQuantity()
        {
            string name = "New product name"; // TODO: Initialize to an appropriate value
            string price = "120"; // TODO: Initialize to an appropriate value
            string description = "This is an example of a valid description which is long enough to be accepted"; // TODO: Initialize to an appropriate value
            string category = "Electronics"; // TODO: Initialize to an appropriate value
            bool hasImages = false; // TODO: Initialize to an appropriate value
            string currency = "Bolivianos"; // TODO: Initialize to an appropriate value
            long productId = 1; // TODO: Initialize to an appropriate value
            string status = "OnSale"; // TODO: Initialize to an appropriate value
            Product expected = null; // TODO: Initialize to an appropriate value
            Product actual;
            actual = ProductRegistrationManager.ModifyProduct(name, price, description, category, hasImages, currency, productId, status, "abc");
            Assert.AreNotEqual(expected, actual);
            Assert.AreEqual(productId, actual.ProductId);
        }

        [TestMethod()]
        [ExpectedException(typeof(ValidationException))]
        public void ModifyProductIncorrectName()
        {
            string name = "Short"; // TODO: Initialize to an appropriate value
            string price = "1500"; // TODO: Initialize to an appropriate value
            string description = "This is an example of a valid description which is long enough to be accepted"; // TODO: Initialize to an appropriate value
            string category = "Electronics"; // TODO: Initialize to an appropriate value
            bool hasImages = false; // TODO: Initialize to an appropriate value
            string currency = "Bolivianos"; // TODO: Initialize to an appropriate value
            long productId = 1; // TODO: Initialize to an appropriate value
            string status = "OnSale"; // TODO: Initialize to an appropriate value
            Product expected = null; // TODO: Initialize to an appropriate value
            Product actual;
            actual = ProductRegistrationManager.ModifyProduct(name, price, description, category, hasImages, currency, productId, status, "1");
            Assert.AreNotEqual(expected, actual);
            Assert.AreEqual(productId, actual.ProductId);
        }


        [TestMethod()]
        [ExpectedException(typeof(ValidationException))]
        public void ModifyProductIncorrectDescription()
        {
            string name = "New product name"; // TODO: Initialize to an appropriate value
            string price = "1500"; // TODO: Initialize to an appropriate value
            string description = "Short"; // TODO: Initialize to an appropriate value
            string category = "Electronics"; // TODO: Initialize to an appropriate value
            bool hasImages = false; // TODO: Initialize to an appropriate value
            string currency = "Bolivianos"; // TODO: Initialize to an appropriate value
            long productId = 1; // TODO: Initialize to an appropriate value
            string status = "OnSale"; // TODO: Initialize to an appropriate value
            Product expected = null; // TODO: Initialize to an appropriate value
            Product actual;
            actual = ProductRegistrationManager.ModifyProduct(name, price, description, category, hasImages, currency, productId, status, "1");
            Assert.AreNotEqual(expected, actual);
            Assert.AreEqual(productId, actual.ProductId);
        }
    }
}
